package cn.wwcyc.controller;

import cn.wwcyc.domain.*;
import cn.wwcyc.mapper.RoleMapper;
import cn.wwcyc.service.RoleService;
import cn.wwcyc.service.RolemenuService;
import cn.wwcyc.service.TjDwService;
import cn.wwcyc.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    RoleMapper rm;

    @Autowired
    RoleService rs;
    @Autowired
    UserService userService;
    @Autowired
    TjDwService tjDwService;

//    @Autowired
//    RolemenuService rolemenuService;

    @Autowired
    RolemenuService rms;

    @GetMapping("/getlist")
    public List<Role> getList(HttpServletRequest request) {
        User user = userService.getCurrentUser(request);
        if(user.getDw() == null)
            return null;
        List<Long> dwIdList = tjDwService.getSelfAndDescendantIdList(user.getDw());
        QueryWrapper<Role> qw = new QueryWrapper<>();
        qw.in("dw", dwIdList);
        qw.orderByDesc("dw");
        List<Role> list = rm.selectList(qw);
        for (Role role : list) {
            role.setDwObj(tjDwService.getDwTreeById(role.getDw()));
        }
        return list;
    }

    @RequestMapping("/getpage")
    public R<IPage<Role>> getPage(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String roleName,
            @RequestParam(required = false) Long dw,
            HttpServletRequest request

    ) {
        User user = userService.getCurrentUser(request);
        if(dw == null)
            dw = user.getDw();

        List<Long> dwIdList = tjDwService.getSelfAndDescendantIdList(dw);

        LambdaQueryWrapper<Role> qw = new LambdaQueryWrapper();
        if (!StringUtils.isBlank(roleName))
            qw.like(Role::getRoleName, roleName);
        qw.in(Role::getDw, dwIdList);

        IPage<Role> page = new Page<>(pageNum, pageSize);
        page = rm.selectPage(page, qw);
        return R.success(page);
    }

    @RequestMapping("/getpageWithDw")
    public R<IPage<Role>> getpageWithDw(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String roleName,
            @RequestParam(required = false) Long dw,
            HttpServletRequest request

    ) {
        User user = userService.getCurrentUser(request);
        if(dw == null)
            dw = user.getDw();
        List<Long> dwIdList = tjDwService.getSelfAndDescendantIdList(dw);
        LambdaQueryWrapper<Role> qw = new LambdaQueryWrapper();
        if (!StringUtils.isBlank(roleName))
            qw.like(Role::getRoleName, roleName);
        qw.in(Role::getDw, dwIdList);
        Long total = rm.selectCount(qw);

        IPage<Role> page = new Page<>(pageNum, pageSize);
        page.setTotal(total);
        page.setPages(page.getTotal() % page.getSize() == 0 ? page.getTotal() / page.getSize() : page.getTotal() / page.getSize() + 1);
        if (pageNum > page.getPages()) {
            page.setCurrent(page.getPages());
        }
        if (pageNum < 1) {
            page.setCurrent(1);
        }
        List<Role> roleList = rm.selectPageWithDwByNameDw(pageNum, pageSize, roleName, dwIdList);
        page.setRecords(roleList);
        return R.success(page);
    }

    @GetMapping("/getpage1")
    public IPage<Role> getPage1(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer population
    ) {
        LambdaQueryWrapper<Role> qw = new LambdaQueryWrapper();
        if (!StringUtils.isBlank(name))
            qw.like(Role::getRoleName, name);

        IPage<Role> page = new Page<>(pageNum, pageSize);
        page = rm.selectPage(page, qw);
        throw new RuntimeException("qwqwqw");

    }

    @PostMapping
    public R<Integer> save(@RequestBody Role role, HttpServletRequest request) {
        User user = userService.getCurrentUser(request);
        if(user.getDw() == null)
            return null;
        if(role.getDw() == null){
            role.setDw(user.getDw());
        }
        Integer i = rm.insert(role);
        return R.success();
    }

    @PostMapping("/edit")
    public R<Integer> updateById(@RequestBody Role role, HttpServletRequest request) {
        User user = userService.getCurrentUser(request);
        if(user.getDw() == null)
            return null;
        if(role.getDw() == null){
            role.setDw(user.getDw());
        }
        Integer i = rm.updateById(role);
        return R.success();
    }

    @DeleteMapping
    public R<Integer> delete(@RequestParam Long id) {
        Integer i = rs.removeRoleById(id);
        return R.success(i);

    }

    @GetMapping("/{roleId}")
    public R<List<Integer>> getRoleMenuList(@PathVariable Integer roleId) {
        List<Integer> menuList = rms.getMenusByRoleId(roleId);
        return R.success(menuList);
    }

    @GetMapping("getMenuGroup")
    public R<List<Integer>> getRoleMenuGroup(@RequestParam Integer roleId) {
//        List<Integer>  menuList = rm.getMenuGroupByRoleId(roleId);
//        return R.success(menuList);
        return null;
    }

    @PostMapping("/setmenu/{roleId}")
    public void setMenu(@PathVariable Integer roleId, @RequestBody List<Integer> menuIds) {
        rms.setRoleMenu(roleId, menuIds);
    }

    @PostMapping("/setmenugroup/{roleId}")
    public R setMenuGroup(@PathVariable Long roleId,
                          @RequestBody RoleMenuGroupDTO roleMenuGroupDTO) {
        rs.setRoleMenuGroup(roleId, roleMenuGroupDTO.getMenuGroupIds(), roleMenuGroupDTO.getSelmenuIds(), roleMenuGroupDTO.getAllmenuIds());

        return R.success(null);
    }
}
